/*
 * @Author: fu.lanyun 
 * @Date: 2019-03-07
 * @Desc: 流程处理
 */
import React from 'react';
import { Form, Input, Select, Modal, Button } from 'antd';

const { Option } = Select;
const { TextArea } = Input;

const formItemLayout1 = {
  labelCol: { span: 6 },
  wrapperCol: { span: 16 },
};
const formItemLayout2 = {
  labelCol: { span: 6 },
  wrapperCol: { span: 10 },
};

@Form.create()
class DealForm extends React.Component {

  handleOk = (e) => {
    if (e && e.preventDefault) e.preventDefault();
    const { form, dispatch, taskId, handleExit } = this.props;
    if (!taskId) return;
    form.validateFields((err, values) => {
      if (err) {
        return;
      }
      const params = { variables: values, taskId };
      dispatch({ type: "FlowHandle/handleTask", payload: params }).then((data) => {
        const { errCode } = data;
        if (errCode === 0) {
          this.handleCancel(true);
          form.resetFields();
          if (handleExit) handleExit();
        }
      });
    });
  }

  handleCancel = (isQuery = false) => {
    const { handleCancel } = this.props;
    handleCancel(false, '', isQuery);
  }

  renderFooter = (isView, confirmLoading) => {
    return (
      <div>
        <Button style={{ marginRight: 10 }} onClick={() => { this.handleCancel(false) }}>{`${isView ? '返回' : '取消'}`}</Button>
        {!isView && <Button loading={confirmLoading} type="primary" onClick={() => { this.handleOk() }}>确定</Button>}
      </div>
    )
  }

  render() {
    const { form, visible, title, data, disable, confirmLoading } = this.props;
    const { handleDetail = {} } = data || {};
    const { getFieldDecorator } = form;
    return (
      <Modal
        title={title || '处理详情'}
        visible={visible}
        onOk={this.handleOk}
        onCancel={() => { this.handleCancel(false) }}
        footer={this.renderFooter(disable, confirmLoading)}
        width='1000px'
        centered
        confirmLoading={confirmLoading}
      >
        <Form>
          <Form.Item
            label="审批结果"
            {...formItemLayout2}
          >
            {getFieldDecorator('approval', {
              initialValue: handleDetail.approval || 'pass',
              rules: [{ required: true, message: '请选择审批结果' }],
            })(
              <Select
                disabled={disable}
                placeholder="请选择审批结果"
              >
                <Option value='pass'>通过</Option>
                <Option value='unpass'>不通过</Option>
              </Select>
            )}
          </Form.Item>
          <Form.Item
            label="审批意见"
            {...formItemLayout1}
          >
            {getFieldDecorator('comment', {
              initialValue: handleDetail.comment || '',
              rules: [{ required: true, message: '请输入审批意见' }],
            })(
              <TextArea disabled={disable} placeholder="请输入审批意见" rows={4} />
            )}
          </Form.Item>
        </Form>
      </Modal>
    );
  }
}
export default DealForm;